<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<base href="${pageContext.request.contextPath }/"> 
<title>CPI大数据采集及编辑</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
	/* 父类改变时调用的函数  */
 	function datachild(){
 		var parentval=$("#datatypeparent").val();
 		if(parentval=="add"){
 			$("#datatypeparent").remove();
 			$("#addparent").append("<input type='text' id='addparentinput' class='input-text-sytle98' placeholder='请添加cpi的数据的父类'>"+
 					"<button onclick='return addparent();' style='margin-left:20px;'>确定</button> <button onclick='gobackaddjsp();'>取消</buttton>");
 		}else{
 			$.ajax({
 				type:"get",
 				url:"DataTypeServlet?parentid="+$("#datatypeparent").val(),
 				async:true,
 				dataType:"json",
 				success:cpidatatype,
 				error:errorinfo
 			});	
 		}
 	}
 	/* 成功的执行函数 */
 	function cpidatatype(data){
 		var str="";
 		$(data).each(function(i){
 			str+="<option value='"+data[i].datatypeid+"'>"+data[i].datatypename+"</option>"
 		});
 		if(data==""){
 			str+="<option>---</option> <option value='addchild'>添加子类</option>";
 		}else{
 			str+="<option value='addchild'>添加子类</option>";
 		}
 		$("#mydatatype").html(str);
 	}
 	/* 发生错误的函数 */
	function errorinfo(){
		alert("请求服务器失败");
	} 
	/* 父类添加中的返回函数 */
	function gobackaddjsp(){
		location.href='getParentDataServlet';
	}
	/* 父类添加中的确定函数 */
	function addparent(){
		if($("#addparentinput").val()==""){
			alert("cpi数据的父类不能为空！");
			return false;
		}else{
			/* $.ajax({
 				type:"get",
 				url:'AddDataTypeParentServlet?addparentinput='+$("#addparentinput").val(),
 				async:true,
 				dataType:"json",
 				success:addparentsuccess,
 				error:errorinfo
 			});	 */
			location.href='AddDataTypeParentServlet?addparentinput='+$("#addparentinput").val();
			return true; 
		}
	}
	/* 浏览器加载 */
	$(function(){
		datachild();
	});
	/* 添加子类的函数 */
	function addchild(){
		var childval=$("#mydatatype").val();
		if(childval=="addchild"){
			$("#mydatatype").remove();
			$("#childtd").append("<input type='text' id='addchildinput' class='input-text-sytle98' placeholder='请添加cpi的数据的子类'>"
			+ "<button onclick='return confirmaddchild();' style='margin-left:20px;margin-right:5px;'>确定</button><button onclick='gobackaddjsp();'>取消</buttton>");
		}
	}
	/* 子类的确认函数  */
	function confirmaddchild(){
		var addchildinput=$("#addchildinput").val();
		if(addchildinput==""){
			alert("添加的子类不能为空！");
			return false;
		}else{
			location.href='AddDataTypeParentServlet?parentid='+$("#datatypeparent").val()+"&addchildinput="+addchildinput;
		}
	}
	/* 添加地区的函数  */
	function addarea(){
		var mydataarea=$("#mydataarea").val();
		if(mydataarea=="addarea"){
			$("#mydataarea").remove();
			$("#areatd").append("<input type='text' id='addareainput' class='input-text-sytle98' placeholder='请添加cpi的数据的来源区域'>"
		+ "<button onclick='return confirmaddarea();' style='margin-left:20px;margin-right:5px;'>确定</button><button onclick='gobackaddjsp();'>取消</buttton>");
		}
	}
	/* 添加数据来源区域的确认函数  */
	function confirmaddarea(){
		var addareainput=$("#addareainput").val();
		if(addareainput==""){
			alert("添加的数据来源区域不能为空！");
			return false;
		}else{
			location.href='AddAreaTypeServlet?addareainput='+addareainput;
			return true;
		}
	}
</script>
</head>
<body>
	<table width="100%" border="0" cellspacing="0" cellpadding="0">
		<tr>
			<td width="153" height="48"><img
				src="images/projectmanager.gif"
				width="153" height="48"></td>
			<td
				background="images/maintitle-bg.gif">&nbsp;</td>
			<td width="80"><img
				src="images/maintitle-right.gif"
				width="80" height="48"></td>
		</tr>
	</table>
	<form id="form1" name="form1" action="CpiRegisterServlet" method="post">
		<input type="hidden" id="id" name="id" value="" />
		<input type="hidden" id="pageIndex" name="id" value="" />
		<table width="100%" border="0" cellspacing="5" cellpadding="0"
			class="message">
			<tbody>
				<tr>
					<td height="35" align="right">CPI数据父类：</td>
					<td id="addparent">
						<select id="datatypeparent" name="datatypeparent"
							class="input-text-sytle98" onChange="datachild()">
								<c:forEach items="${datatype}" var="item">
	                				<option value="${item.datatypeid}">${item.datatypename}</option>
	                			</c:forEach>
	                			<option value='add'>添加父类</option>
						</select>
					</td>
				</tr>
				<tr>
					<td height="35" align="right">CPI数据子类：</td>
					<td id="childtd">
						<select id="mydatatype" name="mydatatype" class="input-text-sytle98" onchange="addchild();">
						</select>
					</td>
				</tr>
				<tr>
					<td width="180" height="35" align="right">数据来源区域：</td>
					<td id="areatd">
						<select id="mydataarea" name="mydataarea" onchange="addarea();" class="input-text-sytle98">
							<c:forEach items="${areatype}" var="item">
                				<option value="${item.areaid}">${item.areaname}</option>
                			</c:forEach>
                			<option value='addarea'>添加区域</option>
						</select>
					</td>
				</tr>
				<tr>
					<td height="35" align="right">数据来源时间：</td>
					<td><input class="input-text-sytle98" id="cpidate" name="cpidate" type="date" required placeholder="请输入数据来源时间"></td>
				</tr>
				<tr>
					<td height="35" align="right">价格：</td>
					<td><input type="number" min="0" step="0.1" name="cpidata" id="cpidata"
						value=""
						class="input-text-sytle98" required placeholder="请输入价格"></td>
				</tr>
				<tr>
					<td height="35" align="right">数据单位：</td>
					<td><select id="databit" name="databit"
						class="input-text-sytle98">
							<option value="500克">500克</option>
							<option value="个">个</option>
							<option value="盒">盒</option>
					</select></td>
				</tr>
 
				<tr>
					<td height="35" align="right" valign="top">备注：</td>
					<td><textarea name="demo" id="demo"
							class="input-text-sytle2010"></textarea></td>
				</tr>
				<tr>
					<td id="info" height="35" align="right" style="color:red;font-size: 12px;"><span id="info">${info}</span></td>
					<td><input name="button" type="submit" class="submitbutton"
						value="保    存" onclick="return check()" /> <input name="button"
						type="button" class="submitbutton" onclick="location.href='CpiDataListServlet';"
						value="返    回" /></td>
				</tr>
				<tr><td></td><td><div style="color:red;"></div></td></tr>
			</tbody>
		</table>
	</form>
</body>
</html>
